﻿Public Class SuiviPopSit

    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Public databaseName As String
    Private Sub SuiviPopSit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Entrer = ""
        Me.ComboBoxIdBat.Visible = False
        Me.ComboBoxIdHab.Visible = False
        Me.TextBoxIdPop.Visible = False
        DataGridViewSuivPop.MultiSelect = False
        DataGridViewSuivPop.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewSuivPop.ReadOnly = True
        majHab()
        majBat()
        miseajour()
    End Sub


    Private Sub majHab()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[HabHumTyp]"
            db.Connecter(databaseName)
            Me.ComboBoxIdHab.Items.Clear()
            Me.ComboBoxNomHab.Items.Clear()
            Me.ComboBoxIdHab.Items.Add("")
            Me.ComboBoxNomHab.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxIdHab.Items.Add(db.rdr(0))
                Me.ComboBoxNomHab.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdHab.SelectedIndex = 0
            Me.ComboBoxNomHab.SelectedIndex = 0
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomHab_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomHab.TextChanged

        If Me.ComboBoxNomHab.FindString(Me.ComboBoxNomHab.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomHab.Text.Length
            Me.ComboBoxNomHab.SelectedIndex = Me.ComboBoxNomHab.FindString(Me.ComboBoxNomHab.Text)
            Me.ComboBoxNomHab.SelectionStart = Pos
            Me.ComboBoxNomHab.SelectionLength = Me.ComboBoxNomHab.Text.Length - Pos
            Me.ComboBoxIdHab.SelectedIndex = Me.ComboBoxNomHab.FindString(Me.ComboBoxNomHab.Text)
        Else
            Me.ComboBoxIdHab.Text = ""
        End If
    End Sub



    Private Sub majBat()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[BatiHumTyp]"
            db.Connecter(databaseName)
            Me.ComboBoxIdBat.Items.Clear()
            Me.ComboBoxNomBat.Items.Clear()
            Me.ComboBoxIdBat.Items.Add("")
            Me.ComboBoxNomBat.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxIdBat.Items.Add(db.rdr(0))
                Me.ComboBoxNomBat.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdBat.SelectedIndex = 0
            Me.ComboBoxNomBat.SelectedIndex = 0
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomBat_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomBat.TextChanged

        If Me.ComboBoxNomBat.FindString(Me.ComboBoxNomBat.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomBat.Text.Length
            Me.ComboBoxNomBat.SelectedIndex = Me.ComboBoxNomBat.FindString(Me.ComboBoxNomBat.Text)
            Me.ComboBoxNomBat.SelectionStart = Pos
            Me.ComboBoxNomBat.SelectionLength = Me.ComboBoxNomBat.Text.Length - Pos
            Me.ComboBoxIdBat.SelectedIndex = Me.ComboBoxNomBat.FindString(Me.ComboBoxNomBat.Text)
        Else
            Me.ComboBoxIdBat.Text = ""
        End If
    End Sub

    Private Sub miseajour()
        Try
            Entrer = ""
            db.myReq = "SELECT * FROM  [dbo].[SuiviPopSit]"
            db.Connecter(databaseName)
            Me.DataGridViewSuivPop.Rows.Clear()
            While db.rdr.Read()
                DataGridViewSuivPop.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5), db.rdr(6), db.rdr(7))
            End While
            db.Disconnecter()
            If DataGridViewSuivPop.RowCount - 1 = 0 Then
                Me.TextBoxEff.Text = ""
                Me.TextBoxMasc.Text = ""
                Me.TextBoxMoy.Text = ""
            Else
                DataGridViewSuivPop.Rows(0).Selected = True
                Me.TextBoxEff.Text = DataGridViewSuivPop.SelectedCells(5).Value
                Me.TextBoxMasc.Text = DataGridViewSuivPop.SelectedCells(6).Value
                Me.TextBoxMoy.Text = DataGridViewSuivPop.SelectedCells(7).Value
                Me.DateTimePickerDate.Text = DataGridViewSuivPop.SelectedCells(4).Value
                Me.ComboBoxNomHab.SelectedItem = Me.ComboBoxidhab.Items.IndexOf(DataGridViewSuivPop.SelectedCells(2).Value)
                Me.ComboBoxNomBat.SelectedItem = Me.ComboBoxIdBat.Items.IndexOf(DataGridViewSuivPop.SelectedCells(3).Value)
                db.Disconnecter()
            End If

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub DataGridViewSuivPop_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewSuivPop.CurrentCellChanged
        If Entrer = "debut" Then
            selection()
        End If
    End Sub
    Private Sub DataGridViewSuivPop_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewSuivPop.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            selection()
        End If
    End Sub

    Private Sub selection()
        Try
            If Not DataGridViewSuivPop.SelectedCells(0).Value.ToString = "" Then

                If Not DataGridViewSuivPop.SelectedCells(4).Value.ToString = "" Then
                    Me.DateTimePickerDate.Text = DataGridViewSuivPop.SelectedCells(4).Value
                Else
                    Me.DateTimePickerDate.Text = ""
                End If
                If Not DataGridViewSuivPop.SelectedCells(5).Value.ToString = "" Then
                    Me.TextBoxEff.Text = DataGridViewSuivPop.SelectedCells(5).Value
                Else
                    Me.TextBoxEff.Text = ""
                End If
                If Not DataGridViewSuivPop.SelectedCells(6).Value.ToString = "" Then
                    Me.TextBoxMasc.Text = DataGridViewSuivPop.SelectedCells(6).Value
                Else
                    Me.TextBoxMasc.Text = ""
                End If
                If Not DataGridViewSuivPop.SelectedCells(7).Value.ToString = "" Then
                    Me.TextBoxMoy.Text = DataGridViewSuivPop.SelectedCells(7).Value
                Else
                    Me.TextBoxMasc.Text = ""
                End If
                If Not DataGridViewSuivPop.SelectedCells(2).Value.ToString = "" Then
                    Me.ComboBoxNomHab.SelectedItem = Me.ComboBoxidhab.Items.IndexOf(DataGridViewSuivPop.SelectedCells(2).Value)
                Else
                    Me.ComboBoxNomHab.SelectedItem = ""
                End If
                If Not DataGridViewSuivPop.SelectedCells(3).Value.ToString = "" Then
                    Me.ComboBoxNomBat.SelectedItem = Me.ComboBoxIdBat.Items.IndexOf(DataGridViewSuivPop.SelectedCells(3).Value)
                Else
                    Me.ComboBoxNomHab.SelectedItem = ""
                End If
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub



    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxEff.Text = ""
        Me.TextBoxMasc.Text = ""
        Me.TextBoxMoy.Text = ""
    End Sub
    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Entrer = ""
            If action = "ajouter" Then
                db.myReq = "INSERT INTO  [dbo].[SUIVIPOPSIT]([Id_Pop],[Id_hht],[Id_TypBati],[DATREC_SPOP],[EFFECTIF_SPOP],[MENNB_SPOP],[MENTAL_SPOP]) VALUES('" & Me.TextBoxIdPop.Text & "','" & Me.ComboBoxidhab.Text & "','" & Me.ComboBoxIdBat.Text & "','" & Me.DateTimePickerDate.Text & "','" & Me.TextBoxEff.Text & "','" & Me.TextBoxMasc.Text & "','" & Me.TextBoxMoy.Text & "')"
                MsgBox(db.myReq)
            Else
                db.myReq = "UPDATE  [dbo].[SUIVIPOPSIT] SET [Id_Pop] = '" & Me.TextBoxIdPop.Text & "',[Id_hht] = '" & Me.ComboBoxidhab.Text & "',[Id_TypBati] = '" & Me.ComboBoxIdBat.Text & "',[DATREC_SPOP] = '" & Me.DateTimePickerDate.Text & "',[EFFECTIF_SPOP] = '" & Me.TextBoxEff.Text & "',[MENNB_SPOP]= '" & Me.TextBoxMasc.Text & "',[MENTAL_SPOP]= '" & Me.TextBoxMoy.Text & "' WHERE [Id_SPop]= '" & Me.DataGridViewSuivPop.SelectedCells(0).Value & "'"

            End If
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        miseajour()
    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Try
            db.myReq = " DELETE FROM  [dbo].[SUIVIPOPSIT]  WHERE [Id_SPop]= '" & Me.DataGridViewSuivPop.SelectedCells(0).Value & "'"
            db.Connecter(databaseName)
            db.Disconnecter()
            miseajour()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub ButtonMod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMod.Click
        action = "update"
    End Sub


    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim typ1 As New TypologieNiv1Form(databaseName)
        typ1.Text = "Catégorie"
        typ1.Table = "HabHumTyp"
        typ1.Visible = True
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim typCat1 As New TypologieNiv1Form(databaseName)
        typCat1.Text = "Catégorie"
        typCat1.Table = "BatiHumTyp"
        typCat1.Visible = True
    End Sub



    Private Sub ComboBoxNomHab_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomHab.MouseDoubleClick
        majHab()
    End Sub

    Private Sub ComboBoxNomBat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomBat.MouseDoubleClick
        majBat()
    End Sub

End Class